package com.diqizu.servlet.admin;



import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;


/**
 * Servlet implementation class imgServlet
 */
@WebServlet("/admin/ImgServlet")
public class ImgServlet extends BaseServlet {
	public void imgUpload(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		// 1.创建SmartUpload
		SmartUpload smart = new SmartUpload();
		// 2.设置字符集
		smart.setCharset("utf-8");
		// 3.初始化
		smart.initialize(getServletConfig(), req, resp);
		try {
			// 4.上传文件
			smart.upload();
			// 5.保存文件到指定位置(save只提供路径，使用原文件名上传文件)
			smart.save("/images");
			// 5.保存文件到指定位置(saveAs可以用指定名称上传文件)
			// 获取文件名
			String filename = smart.getFiles().getFile(0).getFileName();
			out.print(Result.toClient(0, "新图片上传成功", filename));
		} catch (SmartUploadException e) {
			e.printStackTrace();
			out.print(Result.toClient(1, "上传失败", "图片过大"));
		}
		out.flush();
		out.close();
	}
}
